package com.example.fragement;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener, Diy_Fragement_1.OnTitleClickLinstener, Diy_Fragement_2.OntitleClickListener {

    public static final String TAG = MainActivity.class.getSimpleName();
    TextView txt_fragement1;
    TextView txt_fragement2;
    TextView txt_fragement3;
    Diy_Fragement_1 fragement_1;
    Diy_Fragement_2 fragement_2;
    FragmentManager fragmentManager;
    MyNetworkBroadCast mreceiver;
    NetworkInfo networkInfo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);//禁止截屏,需要在加载view之前设置
        setContentView(R.layout.activity_main);
        txt_fragement1 = findViewById(R.id.my_fragement1);
        txt_fragement1.setOnClickListener(this);
        txt_fragement2 = findViewById(R.id.my_fragement2);
        txt_fragement2.setOnClickListener(this);
        txt_fragement3 = findViewById(R.id.my_fragement3);
        txt_fragement3.setOnClickListener(this);

        //动态注册网络监听广播，监听网络状态
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
        mreceiver = new MyNetworkBroadCast();
        registerReceiver(mreceiver, intentFilter);

        //if(networkInfo == )
        //networkInfo=connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.my_fragement1:
                Toast.makeText(MainActivity.this, "已跳转静态加载的Fragment", Toast.LENGTH_SHORT).show();
                startActivity(new Intent(MainActivity.this, Fragement.class));
                break;
            case R.id.my_fragement2:
                Toast.makeText(MainActivity.this, "已动态加载的Fragment", Toast.LENGTH_SHORT).show();
                Fragementdynamic();
                break;
            case R.id.my_fragement3:
                Toast.makeText(MainActivity.this, "已移除动态加载的Fragment", Toast.LENGTH_SHORT).show();
                //RemoveFragementdynamic();
                break;
        }
    }

    /**
     *
     */
    private void RemoveFragementdynamic() {
        if (fragement_1 != null || fragement_2 != null) {
            getSupportFragmentManager().beginTransaction().remove(fragement_1);
            getSupportFragmentManager().beginTransaction().remove(fragement_2);
        }

    }

    /**
     * 动态加载Fragement
     */
    private void Fragementdynamic() {
        /*fragement_1=new Diy_Fragement_1();
        getSupportFragmentManager().beginTransaction().add(R.id.list_container1,fragement_1).commit();*/
        //Activity把值传到Fragment
        fragement_1 = Diy_Fragement_1.newinstance("Fragment1");
        getSupportFragmentManager()
                .beginTransaction()
                .add(R.id.list_container1, fragement_1)
                .commit();
        //设置Fragment1的毁掉监听
        fragement_1.setOnTitleClickLinstener(this);

        //没有传值的时候无显示
        fragement_2 = Diy_Fragement_2.newinstance("Fragment2");
        getSupportFragmentManager()
                .beginTransaction()
                .add(R.id.list_container2, fragement_2)
                .commit();
        //设置Fragment2的毁掉监听
        fragement_2.setOntitleClickListener(this);
    }

    @Override
    public void onFragment1Click(String title) {
        Toast.makeText(MainActivity.this, "你点击了" + title, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onFragment2Click(String title) {
        Toast.makeText(MainActivity.this, "你点击了" + title, Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(mreceiver);
    }

    /********************网络状态监听广播(动态注册)*********************/
    public static class MyNetworkBroadCast extends BroadcastReceiver {

        @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
        @Override
        public void onReceive(Context context, Intent intent) {
            ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo networkInfo_wifi=connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
            NetworkInfo networkInfo_mobile=connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
            if(networkInfo_wifi != null && networkInfo_wifi.isAvailable() && networkInfo_wifi.isConnected()){
                Toast.makeText(context, "当前网络状态WIFI:可用", Toast.LENGTH_SHORT).show();
            }else if(networkInfo_mobile != null && networkInfo_mobile.isAvailable() && networkInfo_mobile.isConnected()) {
                Toast.makeText(context, "当前网络状态移动网络:可用", Toast.LENGTH_SHORT).show();
            }else{
                Toast.makeText(context, "无网络连接", Toast.LENGTH_SHORT).show();
            }

        }
    }


}
